//
//  LZLFindAttentionTableViewCell.swift
//  swiftDemo
//
//  Created by luozhilin on 2018/9/14.
//  Copyright © 2018年 luozhilin. All rights reserved.
//

import UIKit

class LZLFindAttentionTableViewCell: UITableViewCell {
    // 接受传入的模型
    private var eventInfos:EventInfosModel?
    
    // 头像
    private lazy var iconImage:UIImageView = {
        let iconImage = UIImageView()
        iconImage.layer.masksToBounds = true
        iconImage.layer.cornerRadius = 20
        iconImage.image = UIImage(named: "news.png")
        return iconImage
    }()
    
    // 昵称
    private lazy var nameLabel : UILabel={
        let nameLabel = UILabel()
        nameLabel.text = "喜马拉雅好声音"
        return nameLabel
    }()
    
    // 发表的文字
    private lazy var contentLabel:UILabel = {
        let contentLabel = UILabel()
        contentLabel.numberOfLines = 0
        contentLabel.font = UIFont.systemFont(ofSize: 15)
        return contentLabel
    }()
    
    // 发表的时间
    private lazy var timeLabel : UILabel = {
        let timeLabel = UILabel()
        return timeLabel
    }()
    
    // 点赞
    private lazy var priseImage:UIImageView = {
        let priseImage = UIImageView()
        priseImage.image = UIImage(named: "点赞")
        return priseImage
    }()
    
    // 点赞数
    private lazy var priseCount:UILabel = {
        let priseCount = UILabel()
        return priseCount
    }()
    
    // 评论
    private lazy var commentImage:UIImageView = {
        let commentImage = UIImageView()
        commentImage.image = UIImage(named: "评论")
        return commentImage
    }()
    
    // 评论数
    private lazy var commentCount:UILabel = {
        let commentCount = UILabel()
        return commentCount
    }()
    
    // 展示图片的视图
    private lazy var collectionView:UICollectionView = {
        let flayLayout = UICollectionViewFlowLayout()
        flayLayout.itemSize = CGSize(width: (YYScreenWidth - 30)/3, height: (YYScreenWidth - 30)/3)
        flayLayout.minimumLineSpacing = 5
        flayLayout.minimumInteritemSpacing = 5
        flayLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flayLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(LZLPictureCollectionCell.self, forCellWithReuseIdentifier: "LZLPictureCollectionCell")
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = UIColor.white
        return collectionView
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK - 添加子控件
    func setUpUI(){
        self.addSubview(self.iconImage)
        self.iconImage.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(40)
        }
        
        self.addSubview(nameLabel)
        self.nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconImage.snp.right).offset(15)
            make.centerY.equalTo(self.iconImage.snp.centerY)
            make.height.equalTo(30)
        }
        
        self.addSubview(self.contentLabel)
        self.contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconImage)
            make.top.equalTo(self.iconImage.snp.bottom).offset(10)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(50)
        }
        
        self.addSubview(self.timeLabel)
        self.timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconImage)
            make.bottom.equalToSuperview().offset(-15)
            make.height.equalTo(20)
        }
        
        self.addSubview(self.commentCount)
        self.commentCount.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalTo(self.timeLabel)
        }
        
        self.addSubview(self.commentImage)
        self.commentImage.snp.makeConstraints { (make
            ) in
            make.centerY.equalTo(self.timeLabel)
            make.right.equalTo(self.commentCount.snp.left).offset(-10)
            make.height.width.equalTo(20)
        }
        
        self.addSubview(self.priseCount)
        self.priseCount.snp.makeConstraints { (make) in
            make.right.equalTo(self.commentImage.snp.left).offset(-20)
            make.centerY.equalTo(self.timeLabel)
        }
        
        self.addSubview(self.priseImage)
        self.priseImage.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.timeLabel)
            make.right.equalTo(self.priseCount.snp.left).offset(-10)
            make.height.width.equalTo(20)
        }
        
        // 展示发表的图片
        self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(self.contentLabel.snp.bottom).offset(5)
            //  先展示一行的数据，多行的获取到数据再更新
            make.height.equalTo((YYScreenWidth-30)/3+20)
        }
    }
    
    // MARK - 赋值计算
    var eventInfosModel:EventInfosModel?{
        didSet{
            guard let model = eventInfosModel else { return }
            self.iconImage.kf.setImage(with: URL(string: (model.authorInfo?.avatarUrl)!))
            self.nameLabel.text = model.authorInfo?.nickname
            let zanNum:Int = (model.statInfo?.praiseCount)!
            self.priseCount.text = "\(zanNum)"
            let commentNum:Int = (model.statInfo?.commentCount)!
            self.commentCount.text = "\(commentNum)"
            
            self.contentLabel.text = model.contentInfo?.text
            // 计算评论文字的高度
            self.contentLabel.snp.updateConstraints { (make) in
                make.height.equalTo(self.height(for: model.contentInfo))
            }
            self.timeLabel.text = updateTimeToCurrennTime(timeStamp: Double(CGFloat(model.timeline)))
            
            // 计算图片的高度
            self.eventInfos = model
            let picNum = self.eventInfos?.contentInfo?.picInfos?.count ?? 0
            var num:CGFloat = 0
            if picNum > 0 && picNum <= 3 {
                num = 1
            }else if picNum > 3 && picNum <= 6{
                num = 2
            }else if picNum > 6{
                num = 3
            }
            let OnePicHeight = CGFloat((YYScreenWidth-30)/3)
            let picHeight = num * OnePicHeight
            self.collectionView.snp.updateConstraints { (make) in
                make.height.equalTo(picHeight+20)
            }
            self.collectionView.reloadData()
        }
        
    }
    
    func height(for commentModel: FindAContentInfo?) -> CGFloat {
        var height: CGFloat = 30
        guard let model = commentModel else { return height }
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.numberOfLines = 0
        label.text = model.text
        height += label.sizeThatFits(CGSize(width: YYScreenWidth - 30, height: CGFloat.infinity)).height
        return height
    }
    
    // MARK - 计算时间的格式
    func updateTimeToCurrennTime(timeStamp:Double) -> String{
        
        let currentTime = Date().timeIntervalSince1970
        let timeSta:TimeInterval = TimeInterval(timeStamp / 1000)
        
        // 计算时间差
        let reduceTime = currentTime-timeSta
        
        if reduceTime<60 {
            return "刚刚"
        }
        
        //时间差大于一分钟小于60分钟内
        let mins = Int(reduceTime / 60)
        if mins < 60 {
            return "\(mins)分钟前"
        }
        let hours = Int(reduceTime / 3600)
        if hours < 24 {
            return "\(hours)小时前"
        }
        let days = Int(reduceTime / 3600 / 24)
        if days < 30 {
            return "\(days)天前"
        }
        
        //不满足上述条件---或者是未来日期-----直接返回日期
        let date = NSDate(timeIntervalSince1970: timeSta)
        let dfmatter = DateFormatter()
        //yyyy-MM-dd HH:mm:ss
        dfmatter.dateFormat="yyyy年MM月dd日 HH:mm:ss"
        return dfmatter.string(from: date as Date)
    }
    
}



extension LZLFindAttentionTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.eventInfos?.contentInfo?.picInfos?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:LZLPictureCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "LZLPictureCollectionCell", for: indexPath) as! LZLPictureCollectionCell
        cell.picModel = self.eventInfos?.contentInfo?.picInfos?[indexPath.row]
        return cell
    }
    
    //每个分区的内边距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }
    
    //最小 item 间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    //最小行间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: (YYScreenWidth-30)/3, height:(YYScreenWidth-30)/3)
    }
}
